package edu.fsu.cs.SHPE;

import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.drawable.Drawable;
import android.util.Log;

public class SponsorContainer extends ArrayList<Sponsor> {
	private static final long serialVersionUID = -3917704818755762041L;
	
	private ArrayList<Sponsor> viewableList;
	private Sponsor tmpSponsor;
	private String tmpString;
	private final static String TAG = "SHPE.SponsorContainer";
	
	public SponsorContainer(String url) throws ConnectException{
		if(!parseXml(url))
			throw new ConnectException();
		else
			setViewableSponsorsByLevelId(0);
	}
	
	public void setViewableSponsorsByLevelId(long id){
		if(id == 0){
			viewableList = this;
			return;
		}
		
		
		viewableList = new ArrayList<Sponsor>();
		for(Sponsor s : this)
			if(s.containsSponsorshipLevel(id))
				viewableList.add(s);
	}
	
	public ArrayList<Sponsor> getViewableList(){
		return viewableList;
	}
	

	private boolean parseXml(String url){

		Log.i(TAG, "<" + url + ">");

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			// Multiple conference testserver
			//"http://ww2.cs.fsu.edu/~sposaro/test/index.html"

			URL sourceUrl = new URL(url);
			ConferenceXMLHandler conferenceHandler = new ConferenceXMLHandler();
			xr.setContentHandler(conferenceHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));	
			
		} catch (Exception e) {
			return false;
		}
		
		return true;

	}

	public class ConferenceXMLHandler extends DefaultHandler {


		public void startDocument() throws SAXException {
			Log.i(TAG, "Document processing started");

			tmpSponsor = null;
		}


		public void endDocument() throws SAXException {
			Log.i(TAG, "Document processing finished");

			if(tmpSponsor != null)
				add(tmpSponsor);
		}


		public void startElement(String uri, String localName, String qName, Attributes attributes){
			Log.i(TAG, "in element " + localName + " qName " + qName + " uri " + uri);


			if(localName.equalsIgnoreCase("conference-sponsor")){
				if(tmpSponsor != null){
					add(tmpSponsor);
				}

				tmpSponsor = null;
				tmpSponsor = new Sponsor();

				if(attributes.getValue("xlink:logo") != null){
					URL src;
					try {
						src = new URL(attributes.getValue("xlink:logo"));
						tmpSponsor.setLogo(Drawable.createFromStream(src.openStream(), "logo"));
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				tmpSponsor.setId(Long.parseLong(attributes.getValue("id")));
				tmpSponsor.setName(attributes.getValue("name"));
				
			}

			if(localName.equalsIgnoreCase("level"))
				tmpSponsor.setLevelId(Long.parseLong(attributes.getValue("id")));
		}

		public void characters(char[] ch, int start, int length) throws SAXException{
			tmpString = new String(ch, start, length);
			Log.i(TAG, "string " + tmpString);
		}

		public void endElement(String uri, String localName, String qName)
		throws SAXException {
			Log.i(TAG, "endElement " + localName);

			if(localName == null || tmpString == null || tmpSponsor == null)
				return;

			if(localName.equalsIgnoreCase("website"))
				tmpSponsor.setWebsite(tmpString);
			else if(localName.equalsIgnoreCase("recruiting"))
				tmpSponsor.setRecruiting(Sponsor.parseBoolean(tmpString));
			else if(localName.equalsIgnoreCase("accepts-international-students"))
				tmpSponsor.setAcceptsInternationStudents(Sponsor.parseBoolean(tmpString));
		}

	}

}
